package com.kitchen.dao;

import com.kitchen.entity.Dish;
import com.kitchen.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜品DAO类
 * 使用List集合存储查询结果
 */
public class DishDAO {
    
    /**
     * 查询所有可用菜品
     */
    public List<Dish> findAllAvailable() {
        List<Dish> dishes = new ArrayList<>();
        String sql = "SELECT * FROM dish WHERE status = '可用' ORDER BY dish_type, dish_id";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                Dish dish = mapResultSetToDish(rs);
                dishes.add(dish);
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询菜品失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt, rs);
        }
        
        return dishes;
    }
    
    /**
     * 根据ID查询菜品
     */
    public Dish findById(Integer dishId) {
        String sql = "SELECT * FROM dish WHERE dish_id = ?";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, dishId);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return mapResultSetToDish(rs);
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询菜品失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt, rs);
        }
        
        return null;
    }
    
    /**
     * 根据类型查询菜品
     */
    public List<Dish> findByType(String dishType) {
        List<Dish> dishes = new ArrayList<>();
        String sql = "SELECT * FROM dish WHERE dish_type = ? AND status = '可用'";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, dishType);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                dishes.add(mapResultSetToDish(rs));
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询菜品失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt, rs);
        }
        
        return dishes;
    }
    
    /**
     * 添加菜品
     */
    public boolean add(Dish dish) {
        String sql = "INSERT INTO dish (dish_name, dish_type, price, preparation_time, status) VALUES (?, ?, ?, ?, ?)";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, dish.getDishName());
            pstmt.setString(2, dish.getDishType());
            pstmt.setBigDecimal(3, dish.getPrice());
            pstmt.setInt(4, dish.getPreparationTime());
            pstmt.setString(5, dish.getStatus() != null ? dish.getStatus() : "可用");
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new RuntimeException("添加菜品失败: " + e.getMessage(), e);
        } finally {
            DatabaseUtil.close(conn, pstmt);
        }
    }
    
    private Dish mapResultSetToDish(ResultSet rs) throws SQLException {
        Dish dish = new Dish();
        dish.setDishId(rs.getInt("dish_id"));
        dish.setDishName(rs.getString("dish_name"));
        dish.setDishType(rs.getString("dish_type"));
        dish.setPrice(rs.getBigDecimal("price"));
        dish.setPreparationTime(rs.getInt("preparation_time"));
        dish.setStatus(rs.getString("status"));
        if (rs.getTimestamp("create_time") != null) {
            dish.setCreateTime(rs.getTimestamp("create_time").toLocalDateTime());
        }
        return dish;
    }
}

